package net.zoneland.knowledge.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import net.zoneland.knowledge.controller.bean.SanHuiYiKeVo;
import net.zoneland.knowledge.dao.DiYiYiTiMapper;
import net.zoneland.knowledge.enums.CompanyEnum;
import net.zoneland.knowledge.model.DiYiYiTi;
import net.zoneland.knowledge.service.DiYiYiTiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 第一议题业务实现类.
 * @author zx
 */
@Service
public class DiYiYiTiServiceImpl extends AbstractZhiXumServiceImpl implements DiYiYiTiService {

    @Autowired
    private DiYiYiTiMapper diYiYiTiMapper;

    @Override
    public List<DiYiYiTi> searchByParam(final SanHuiYiKeVo sanHuiYiKeVo) {
        checkParam(sanHuiYiKeVo);
        sanHuiYiKeVo.setPage();
        return diYiYiTiMapper.search(sanHuiYiKeVo);
    }

    @Override
    public PageInfo searchToPage(final SanHuiYiKeVo sanHuiYiKeVo) {
        checkParam(sanHuiYiKeVo);
        final Page<DiYiYiTi> page = sanHuiYiKeVo.setPage();
        diYiYiTiMapper.search(sanHuiYiKeVo);
        return new PageInfo<>(page.getResult());
    }

    @Override
    public List<DiYiYiTi> searchList(final SanHuiYiKeVo sanHuiYiKeVo) {
        checkParam(sanHuiYiKeVo);
        final List<DiYiYiTi> list = diYiYiTiMapper.search(sanHuiYiKeVo);
        list.sort((o1, o2)  -> CompanyEnum.comparator(o1.getCompanyName(), o2.getCompanyName()));
        return list;
    }

    @Override
    public DiYiYiTi getInfoById(final String id) {
        return diYiYiTiMapper.getInfoById(id);
    }

    @Override
    protected void checkParam(SanHuiYiKeVo sanHuiYiKeVo) {
        handlerParam(sanHuiYiKeVo);
        meetingNameToList(sanHuiYiKeVo);
        final LocalDate endTime = sanHuiYiKeVo.getMeetingEndTime();
        //有结束时间无开始时间时，取当前时间.
        Optional.ofNullable(endTime)
                .ifPresent(s -> sanHuiYiKeVo.setMeetingStartTime(Optional.ofNullable(sanHuiYiKeVo.getMeetingStartTime()).
                        orElseGet(LocalDate::now)));
    }
}
